//
//  TSCVCApplyCreateMall.m
//  Tsc_IOS
//
//  Created by Rio on 15-6-3.
//  Copyright (c) 2015年 idream. All rights reserved.
//

#import "TSCVCApplyCreateMall.h"
#import "TSCConstants.h"
#import "TSCMyUIUtil.h"
#import "TSCMyUtil.h"
#import "TSCMyCheckUtil.h"
#import "TSCMyNetworkUtil.h"
#import "TSCMyHttpUtil.h"

@interface TSCVCApplyCreateMall () {
    // 输入项
    NSInteger _iCityId;
    
    
}

@property (weak, nonatomic) IBOutlet UITextField *tfMallName;
@property (weak, nonatomic) IBOutlet UIButton *btnSubmit;

@end

@implementation TSCVCApplyCreateMall

#pragma mark 校验并获取输入
- (void) getVCInput:(NSNotificationCenter *)notification {
    [super getVCInput:notification];
    
    _iCityId = [[_inputDic objectForKey:AEKI_CITY_ID] intValue];
    
    NSAssert(_iCityId != 0, @"%s: 输入参数必须包括：%@！", __FUNCTION__, AEKI_CITY_ID);
}

+ (id) vcApplyCreateMall {
    UIStoryboard * sb = [UIStoryboard storyboardWithName:UI_STORYBOARD_NAME_MAIN bundle:[NSBundle mainBundle]];
    return [sb instantiateViewControllerWithIdentifier:UI_VC_NAME_APPLY_CREATE_MALL];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 设置自定义导航栏
    _nbc = [[TSCMyNaviBarCreator alloc]initAndCreateWithViewController:self hasGoBackButton:YES goBackVC:nil hasAppLogo:YES titleResourceKey:@"title_apply_create_mall" iconResourceNameArray:nil iconClickedListenerArray:nil];
    
    // ===== 初始化UI
    
    // 初始化文字说明
    _tfMallName.placeholder = NSLocalizedString(@"hint_input_mall_name", nil);
    
    // 初始化提交按钮
    [_btnSubmit setTitle:NSLocalizedString(@"btn_submit", nil) forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

// 注册事件——提交创建商场申请
- (IBAction)btnSubmitTouchUpInside:(id)sender {
    // 收回键盘
    [self resignAllFirstResponder];
    
    NSString * mallName = [TSCMyUtil trim:_tfMallName.text];
    
    // 检查商场名称
    if ([TSCMyCheckUtil checkWithString:mallName regexType:RegexTypeMallName] == false) {
        [TSCMyUIUtil popErrWithResourceKey:@"err_format_mall_name"];
        return;
    }
    
    // 先检查网络情况，若不可用，直接返回
    if ([TSCMyNetworkUtil checkNetwork] == false) {
        [TSCMyUIUtil popErrWithResourceKey:@"err_network_unavailable"];
        return;
    }
    
    // 提示用户正在提交
    [self showDialogWithResourceKey:@"content_submitting" isProcessing:YES];
    
    // 提交创建商场申请
    [self applyMall_PP1:mallName];
}

#pragma mark 私有方法
/* private */ - (void) applyMall_PP1:(NSString *)mallName {
    
    // 拼装请求
    NSMutableDictionary * reqDic = [[NSMutableDictionary alloc] init];
    [reqDic setObject:[_ud objectForKey:UD_USER_ID] forKey:@"user_id"];
    [reqDic setObject:[_ud objectForKey:UD_LOGIN_KEY] forKey:@"login_key"];
    [reqDic setObject:mallName forKey:@"name"];
    [reqDic setObject:[NSNumber numberWithInteger:_iCityId] forKey:@"city_id"];
    
    // 发送请求
    [TSCMyHttpUtil postWithUrl:URL_APPLY_MALL dictionary:reqDic completionHandler:^(ServerRespType serverRespType, NSString * respString, NSDictionary *respDictionary, NSInteger code, NSString *msg, NSDictionary *contentDictionary) {
        // 服务器返回错误处理
        switch (serverRespType) {
            case ServerRespTypeSuccess:
                break;
            case ServerRespTypeErrNil:
            case ServerRespTypeErrJson: {
                [self dissmisDialog];
                [TSCMyUIUtil popErrWithResourceKey:@"err_web_server_unavailable"];
                return;
            }
            case ServerRespTypeErrCode:{
                [self dissmisDialog];
                [TSCMyUIUtil popErrWithString:msg];
                return;
            }
            default:
                [self dissmisDialog];
                [TSCMyUIUtil popErrWithResourceKey:@"err_system"];
                return;
        }
        
        // 继续后续逻辑...
        [self applyMall_PP2:msg];
    }];
    
}

/* private */ - (void) applyMall_PP2:(NSString *)serverRespMsg {
    TSCMyDialog * dialog = [[TSCMyDialog alloc]init];
    dialog.msgText = serverRespMsg;
    [_dialog replaceContentViewWithOtherDialog:dialog];
    
    // 稍后关闭界面
    [TSCMyUIUtil popVCLaterWithNVC:[self navigationController] animated:YES];
}

/* private */ - (void) resignAllFirstResponder {
    [_tfMallName resignFirstResponder];
}

// return键收回键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

// 点击空白区域收回键盘
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self resignAllFirstResponder];
}

@end
